package cn.ven.controller;

import cn.ven.entity.*;
import cn.ven.service.OrderService;
import cn.ven.tool.RespJson;
import cn.ven.tool.Token;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/fore/ord")
public class OrdForeController {

    private static Logger log = LoggerFactory.getLogger(OrdForeController.class);

    @Autowired
    OrderService orderService;

    //查找用户自身下的某一个订单号
    @RequestMapping("/findInfo/{ordid}")
    @ResponseBody
    public Map<String,Object> findInfo(@PathVariable("ordid")Integer ordid){
        List<OrdItem> ordItems = orderService.findOrdItem(Token.getUid(), ordid);
        return RespJson.pack(200,"",ordItems);
    }

    //根据ordid添加地址和电话
    @RequestMapping("/updateOrder")
    @ResponseBody
    public Map<String,Object> updateOrder(@RequestBody Order order){
        orderService.updateOrder(order);//更新
        return RespJson.pack(200,"添加成功",null);
    }

    //更新付款标识
    @RequestMapping("/addPay/{ordid}")
    @ResponseBody
    public Map<String,Object> addPay(@PathVariable("ordid")Integer ordid){
        orderService.addPay(ordid);
        return RespJson.pack(200,"付款成功",null);
    }

    //查看该用户的所有的订单头信息
    @RequestMapping("/findOrdLines")
    @ResponseBody
    public Map<String,Object> findOrdLines(Date tstart,
                                           Date tend,Integer ispay){
        Map<String,Object> map =new HashMap();
        if (ispay!=null) map.put("ispay",ispay);
        if (tstart!=null) map.put("tstart",tstart);
        if (tend!=null) map.put("tend",tend);
        Integer uid = Token.getUid();
        if (uid==null){
            return RespJson.pack(401,"Token过期，请重新登录",null);
        }
        map.put("uid",uid);
        List<OrderLine> ordLines =null;
        try {
             ordLines = orderService.findOrdLines(map);
        }catch (Exception e){
            RespJson.pack(500, "查询数据有误....", null);
        }
        return RespJson.pack(200,"",ordLines);
    }

    //分页查看该用户的所有订单头信息
    @RequestMapping("/findOrdLinesPage/{start}/{num}")
    @ResponseBody
    public Map<String,Object> findOrdLine(Date tstart,
                                          Date tend,Integer ispay,
                                          @PathVariable("start")Integer start,
                                          @PathVariable("num")Integer num){
        Map<String,Object> map =new HashMap();  //用来查询用的map，走键值对比较方便
        Map<String,Object> info =new HashMap<>(); //用来存放数据总条数以及查询结果
        if (ispay!=null) map.put("ispay",ispay);  //当有查询付款或者没付款的时候，放入map里面
        if (tstart!=null) map.put("tstart",tstart); //有填起始时间
        if (tend!=null) map.put("tend",tend); //有末时间
        if (start!=null) map.put("start",start-1);
        if (num!=null) map.put("num",num);
        Integer uid = Token.getUid(); //获取uid
        if (uid==null){ //当用户id没有的时候，默认没登录
            return RespJson.pack(401,"Token过期，请重新登录",null);
        }
        map.put("uid",uid); //有登录把uid加进去
        List<OrderLine> ordLines =null;
        try {
            ordLines = orderService.findOrdLines(map);
        }catch (Exception e){
            RespJson.pack(500, "查询数据有误....", null);
        }
        info.put("ordLines",ordLines);
        info.put("total",orderService.getOrderLinesCount(map)); //总条数
        return RespJson.pack(200,"",info);
    }

    //根据订单号获取该订单明细以及书本信息
    @RequestMapping("/findItemInfoByOrdid/{ordid}")
    @ResponseBody
    public Map<String,Object> findItemInfoByOrdid(@PathVariable("ordid")Integer ordid){
        log.info("ordid==="+ordid);
        if (Token.getAccount()==null||Token.getAccount()instanceof MyUser ==false){ //当用户为null的时候，也就是没有登录,或者拿到的不是MyUser对象
            return RespJson.pack(401,"Token过期，请重新登录.....",null);
        }
        List<ItemBook> itemBooks = null;
        try {
            itemBooks=orderService.findItemInfoByOrdid(ordid);
        }catch (Exception e){
            return RespJson.pack(500,"查询数据有误",null);
        }
        return RespJson.pack(200,"",itemBooks);
    }







}
